 <!-- 管理员登录页 -->
<template>
  <div id="register">
    <div class="register-box">
      <!-- 管理登录 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model.number="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input
            class="code-input"
            type="password"
            v-model="ruleForm.code"
            autocomplete="off"
          ></el-input>
          <div
            class="codeImg"
            @click="getcode()"
            style="width: 100px; height: 50px; background: white; float: right"
          >
            <img class="autoCode" :src="codeImg" alt="验证码" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="LoginForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "../request/axios";
import adminModel from "../request/admin";
import cookie from "js-cookie";
export default {
  data() {
    //验证规则
    var name = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        if (/[\w\u4e00-\u9fa5]{2,6}/.test(value)) {
          return callback();
        } else {
          return callback(new Error("用户名必须为6-12位字母/数字/中文"));
        }
      }
    };

    var pass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (/^[a-z0-9]{5,12}$/.test(value)) {
          callback();
        } else {
          callback(new Error("密码必须为字母开头6-12位数字字母组成"));
        }
      }
    };

    var code = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else {
        if (/^[a-z0-9]{5,12}$/.test(value)) {
          callback();
        } else {
          callback(new Error("验证码有误"));
        }
      }
    };
    return {
      // 验证码图片
      codeImg: "",
      // 普通用户字段
      ruleForm: {
        name: "",
        pass: "",
        code: "",
      },
      rules: {
        name: [{ validator: name, trigger: "blur" }],
        pass: [{ validator: pass, trigger: "blur" }],
        code: [{ validator: code, trigger: "blur" }],
      },
    };
  },

  //页面的事件和方法
  methods: {
    // 登录验证
    LoginForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          adminModel
            .adminLogin({
              userName: this.ruleForm.name,
              password: this.ruleForm.pass,
              txt: this.ruleForm.code,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("登录成功!");
                //注册成功跳转首页
                this.$router.push({ path: "/list" });
                // 将用户信息存到本地
                let userinfo = res.data.result;
                cookie.set("satoken", userinfo.token);
                userinfo = JSON.stringify(userinfo);
                window.localStorage.setItem("userinfo", userinfo);
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          return false;
        }
      });
    },

    //点击获取验证码
    getcode() {
      this.codeImg =
        axios.defaults.baseURL +
        "file/getImg" +
        "?data = " +
        new Date().getTime();
    },

    //重置信息
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
      console.log("111");
    },
  },

  //生命周期（页面加载时执行）
  created() {
    // 页面加载时生成验证码
    this.codeImg =
      axios.defaults.baseURL +
      "file/getImg" +
      "?data = " +
      new Date().getTime();
  },
};
</script>

<style lang="scss" scoped>
@import "../css/RegisterAndLogin.scss";
.autoCode {
  width: 100%;
  height: 100%;
}
</style>